package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/comment")
public class DtsAdminCommentController {


    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsCommentService commentService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsComment comment) {
        Subject subject = SecurityUtils.getSubject();
        if(!subject.hasRole("超级管理员")) {
            return ResponseUtil.fail(-1, "只有超级管理员删除!! ");
        }
        commentService.delete(comment);
        return ResponseUtil.ok();
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(name = "limit", required = false, defaultValue = "10") Integer limit,
                       String sort, String order, Integer userId,
                       @RequestParam(name = "valueId", required = false) Integer goodsId) {

        //判断 当前登录用户是否是品牌制造商， 如果是，只查询品牌商品的评论数据

        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;
        if(subject.hasRole("品牌制造商")) {
            //查询该用户对应的品牌id
            brandIds = brandService.queryByAdminId(admin.getId());
        }

        IPage<DtsComment> ipage = commentService.queryPage(page, limit, sort, order, goodsId, userId, brandIds);

        Map<String, Object> data = new HashMap<>();
        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return ResponseUtil.ok(data);
    }

}
